package com.boot.ys.mybatisplus.controller;


import com.boot.ys.mybatisplus.entity.Caidan;
import com.boot.ys.mybatisplus.entity.Role;
import com.boot.ys.mybatisplus.entity.dto.CaiDanDto;
import com.boot.ys.mybatisplus.result.Result;
import com.boot.ys.mybatisplus.service.CaidanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yang Sheng
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/caidan")
public class CaidanController {

    @Autowired
    private CaidanService caidanService;

    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Caidan> caidansList = caidanService.selectAll();
        return Result.ok(caidansList);
    }


    @PostMapping("/deleteById")
    public Result deleteById(@RequestBody Caidan caidan){
        int i = caidanService.deleteById(caidan);
        if (i>0){
            return Result.ok();
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/addCaidan")
    public Result addCaidan(@RequestBody Caidan caidan){
        int i = caidanService.addCaidan(caidan);
        if (i>0){
            return Result.ok();
        }
        return Result.fail("添加失败");
    }

    @PostMapping("/updateCaidan")
    public Result updateCaidan(@RequestBody Caidan caidan){
        int i = caidanService.updateCaidan(caidan);
        if (i>0){
            return Result.ok();
        }
        return Result.fail("更新失败");
    }


    @PostMapping("/getTree")
    public Result getTree(@RequestBody CaiDanDto caiDanDto){

        List<CaiDanDto> tree = caidanService.getTree(caiDanDto);

        return Result.ok(tree);
    }






}
